<!DOCTYPE html>
<html>
<head>
<title>projectTable.html</title>

<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

<link rel="stylesheet" href="/Hospital/lib/layui-v2.1.6/layui/css/layui.css" media="all">
<link rel="stylesheet" href="/Hospital/resources/css/table.css" media="all">
</head>

<body>
	<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
		<legend>项目管理</legend>
		<button id="btn_add" class="layui-btn btn_right">
			<i class="layui-icon">&#xe61f;</i> 添加
		</button>
		<select id="sel_category" name="sel_category" class="sel_category">
			<option value="1">治疗项目</option>
			<option value="2">费用项目</option>
		</select>
	</fieldset>
	<table id="projectTable" class="layui-table table" lay-filter="projectTable"></table>
</body>
<script src="/Hospital/lib/jquery.min.js" charset="utf-8"></script>
<script src="/Hospital/lib/layui-v2.1.6/layui/layui.js" charset="utf-8"></script>
<script src="/Hospital/resources/js/util.js" charset="utf-8"></script>
<script id="operator" type="text/html">
	<a class="layui-btn layui-btn-mini" lay-event="edit">编辑</a>
</script>
<script type="text/javascript">
	var loginUser = JSON.parse(localStorage.getItem("loginUser"));
	layui.use([ "table", "layer", "element" ], function() {
		var $ = layui.jquery;
		var table = layui.table;
		var element = layui.element;
		var layer = layui.layer;
		
		initFrame();
		
		function initFrame() {
			
			if(loginUser.permission.createProject != 5) {	//如果不是管理员隐藏添加按钮
				hide("#btn_add");
			}
			
			initTreatTable();//初始化治疗表格	
			categoryEvent();//sel_category 监听事件
			btnAddEvent(); //新增项目按钮 监听事件
		}
		
		
		//初始化治疗表格
		function initTreatTable() {
			localStorage.setItem("projectTable_category", 1);
			//表格初始化
			table.render({
				id : "projectTable",
				elem : "#projectTable",
				page : true,
				limit : 10,
				center : true,
				cols : createTreatCols(),
				url : "/Hospital/projectController/getAllTreatmentProject",
				method : "post",
				where : "",
				response : {
					statusName : "code",
					statusCode : "200",
					countName : "total",
					msgName : "msg",
					dataName : "result",
				}
			});
			
			//监听工具条  lay-filter
			table.on('tool(projectTable)', function(obj){
				//将当行数据写入local
				var data = obj.data;
				//console.log("projectTable.html/obj.data");
				//console.log(data);
				localStorage.setItem("projectDetail", JSON.stringify(data));
				
				//编辑按钮
				if(obj.event === "edit") {
					editClick(data);
				}
			});
		}
		
		//初始化费用表格
		function initFundTable() {
			localStorage.setItem("projectTable_category", 2);
			var height = window.innerHeight*0.9;
			var loginUser = JSON.parse(localStorage.getItem("loginUser"));
			//表格初始化
			table.render({
				id : "projectTable",
				elem : "#projectTable",
				page : true,
				limit : 10,
				center : true,
				cols : createFundCols(),
				url : "/Hospital/projectController/getAllFundProject",
				method : "post",
				where : "",
				response : {
					statusName : "code",
					statusCode : "200",
					countName : "total",
					msgName : "msg",
					dataName : "result",
				}
			});
			
			//监听工具条  lay-filter
			table.on('tool(projectTable)', function(obj){
				//将当行数据写入local
				var data = obj.data;
				//console.log("projectTable.html/obj.data");
				//console.log(data);
				localStorage.setItem("projectDetail", JSON.stringify(data));
				
				//编辑按钮
				if(obj.event === "edit") {
					editClick(data);
				}
			});
		}
		
		//编辑按钮点击事件
		function editClick(data) {
			//设置弹窗大小
			var area;
			var category = localStorage.getItem("projectTable_category");
			if(category == 1) {
				//治疗项目
				area = ["530px","552px"];
			}else if(category ==2) {
				//费用项目
				area = ["372px","360px"];
			}
			
			//设置projectTable_operator: 1(新增);2(编辑);
			localStorage.setItem("projectTable_operator", 2);
			
			layer.open({
				type : 2,
				title : "职位编辑窗口",
				area : area,
				content : '/Hospital/views/projectManager/projectDetail.html',
				btn : ["确认","取消"],
				btnAlign : "r",
				resize : true,
				yes : function(index, layero) {
					var param = $(layero).find("iframe")[0].contentWindow.getParam();
					var mainFrame = index;
					//console.log("projectTable.html/param");
					//console.log(param);
					//console.log("projectTable.html/obj.data");
					//console.log(data);
					 //数据验证
					if(!vertify(param)) {
						return;
					}
					 
					 //如果没有修改直接显示修改成功
					if(isAlter(param,data)) {
					 	layer.close(mainFrame);
					 	localStorage.removeItem("projectDetail");
						localStorage.removeItem("projectTable_operator");
					 	return;
					}
					 
					//插入数据库
					insertToDB(param,mainFrame);
				},
				cancel : function(index, layero) {
					//左上角X关闭按钮
					localStorage.removeItem("projectDetail");
					localStorage.removeItem("projectTable_operator");
					layer.close(index);
				},
				btn2 : function(index, layero) {
					//取消按钮
					localStorage.removeItem("projectDetail");
					localStorage.removeItem("projectTable_operator");
					layer.close(index);
				}
			});
		}
		
		//插入数据库
		function insertToDB(param,mainFrame) {
			//console.log("insertToDB param");
			//console.log(param);
			layer.confirm('确认修改？', {icon: 3, title:'提示'}, function(index){
				 $.ajax({
					type : "post",
					url : "/Hospital/projectController/alterProject",
					data : param,
					success : function(response) {
						if(response.code != 200) {
							console.error("alterPosition.response");
							console.error(response);
							return;
						}
						if(response.code == 200) {
							layer.msg("修改成功",{icon:1,time:1000});
						}
						//关闭窗口
						layer.close(index);
						layer.close(mainFrame);
						
						//重载表格
						var category = localStorage.getItem("projectTable_category");
						var url;
						if(category == 1) {
							initTreatTable();
						}
						if(category == 2) {
							initFundTable();
						}
					}
				});
			});
		}
		
		//sel_category 监听事件
		function categoryEvent() {
			$("#sel_category").change(function() {
				//console.log($(this).val());
				if ($(this).val() == 1) {
					//治疗项目
					initTreatTable();
				}
				if ($(this).val() == 2) {
					//费用项目
					initFundTable();
				}
			});
		}
		
		//新增项目按钮 监听事件
		function btnAddEvent() {
			$("#btn_add").on("click",function() {
				var area; //弹窗大小
				var title = ""; //弹窗标题
				var category = localStorage.getItem("projectTable_category");
				
				if(category == 1) {
					//治疗项目
					area = ["540px","455px"];
					title = "新增治疗项目";
				}else if(category ==2) {
					//费用项目
					area = ["412px","260px"];
					title = "新增费用项目";
				}
				//projectTable_operator: 1(新增);2(编辑);
				localStorage.setItem("projectTable_operator", 1);
				layer.open({
					type : 2,
					title : title,
					area : area,
					content : "/Hospital/views/projectManager/projectDetail.html",
					btn : ["确认","取消"],
					btnAlign : "r",
					resize : true,
					yes : function(index,layero) {
						var param = $(layero).find("iframe")[0].contentWindow.getParam();
						console.log("btnAddEvent.param");
						console.log(param);
						//验证数据 false:数据不合法;true:数据合法
						if(!vertify(param)) {
							return;
						}
						
						$.ajax({
							type : "post",
							url : "/Hospital/projectController/createProject",
							data : param,
							success : function(response) {
								if(response.code != 200) {
									console.log("createProject.response");
									console.log(response);
								}
								layer.close(index);
								//重载表格
								var category = localStorage.getItem("projectTable_category");
								var url;
								if(category == 1) {
									initTreatTable();
								}
								if(category == 2) {
									initFundTable();
								}
							}
						});
					},
					cancel : function(index,layero) {
						layer.close(index);
					},
					btn2 : function(index,layero) {
						layer.close(index);
					}
				});
			});
		}
	});
	
	function createTreatCols() {
		var permission = JSON.parse(localStorage.getItem("loginUser")).permission;
		//如果没有修改项目权限
		if(permission.alterPosition != 5) {
			var tableWidth=window.innerWidth*0.109;
			cols = [[
		                {field:'projectName',width:tableWidth,title:'项目',align:'center'},
		                {field:'creatorName',width:tableWidth,title:'创建人',align:'center'},
		                {field:'unitPrice',width:tableWidth,title:'治疗单价',sort:true,align:'center'},
		                {field:'doctorCost',width:tableWidth,title:'医生抽成%',sort:true,align:'center'},
		                {field:'excutorCost',width:tableWidth,title:'执行者抽成%',sort:true,align:'center'},
		                {field:'clinicCost',width:tableWidth,title:'分院抽成%',sort:true,align:'center'},
		                {field:'status',width:tableWidth,title:'状态',sort:true,align:'center'},
		                {field:'alterTime',width:tableWidth,title:'修改时间',sort:true,align:'center'},
		                {field:'createTime',width:tableWidth,title:'创建时间',sort:true,align:'center'},
		           ]];
			return cols;
		}
		//如果有修改项目权限
		var tableWidth=window.innerWidth*0.104;
		cols = [[
					{field:'projectName',width:tableWidth,title:'项目',align:'center'},
					{field:'creatorName',width:tableWidth,title:'创建人',align:'center'},
					{field:'unitPrice',width:tableWidth,title:'治疗单价',sort:true,align:'center'},
	                {field:'doctorCost',width:tableWidth,title:'医生抽成%',sort:true,align:'center'},
	                {field:'excutorCost',width:tableWidth,title:'执行者抽成%',sort:true,align:'center'},
	                {field:'clinicCost',width:tableWidth,title:'分院抽成%',sort:true,align:'center'},
	                {field:'status',width:tableWidth,title:'状态',sort:true,align:'center'},
					{field:'alterTime',width:tableWidth,title:'修改时间',sort:true,align:'center'},
					{field:'createTime',width:tableWidth,title:'创建时间',sort:true,align:'center'},
					{toolbar:'#operator',width:70,title:'操作',fixed: 'right'},
	           ]];
		return cols;
	}
	
	//生成费用项目表格列
	function createFundCols() {
		var permission = JSON.parse(localStorage.getItem("loginUser")).permission;
		//如果没有修改项目权限
		if(permission.alterPosition != 5) {
			var tableWidth=window.innerWidth*0.164;
			cols = [[
		                {field:'projectName',width:tableWidth,title:'项目',align:'center'},
		                {field:'creatorName',width:tableWidth,title:'创建人',align:'center'},
		                {field:'type',width:tableWidth,title:'支出类别',align:'center'},
		                {field:'status',width:tableWidth,title:'状态',sort:true,align:'center'},
		                {field:'alterTime',width:tableWidth,title:'修改时间',sort:true,align:'center'},
		                {field:'createTime',width:tableWidth,title:'创建时间',sort:true,align:'center'},
		           ]];
			return cols;
		}
		//如果有修改项目权限
		var tableWidth=window.innerWidth*0.155;
		cols = [[
					{field:'projectName',width:tableWidth,title:'项目',align:'center'},
					{field:'creatorName',width:tableWidth,title:'创建人',align:'center'},
					{field:'type',width:tableWidth,title:'支出类别',sort:true,align:'center'},
					{field:'status',width:tableWidth,title:'状态',sort:true,align:'center'},
					{field:'alterTime',width:tableWidth,title:'修改时间',sort:true,align:'center'},
					{field:'createTime',width:tableWidth,title:'创建时间',sort:true,align:'center'},
					{toolbar:'#operator',width:70,title:'操作',fixed: 'right'},
	           ]];
		return cols;
	}
	
	//验证数据 false:数据不合法;true:数据合法
	function vertify(param) {
		//console.log("vertify.param");
		//console.log(param);
		var doctorCost = parseFloat(param.doctorCost);
		var clinicCost = parseFloat(param.clinicCost);
		var excutorCost = parseFloat(param.excutorCost);
		if(param.name == null || param.name == "") {
			layer.msg("项目名称不合法", {icon : 2,time : 1000,});	
			return false;
		}
		if(doctorCost < 0 || doctorCost > 100) {
			layer.msg("医生抽成不合法", {icon : 2,time : 1000,});	
			return false;
		}
		if(excutorCost < 0 || excutorCost > 100) {
			layer.msg("执行者抽成不合法", {icon : 2,time : 1000,});	
			return false;
		}
		if(clinicCost < 0 || clinicCost > 100) {
			layer.msg("分院抽成不合法", {icon : 2,time : 1000,});	
			return false;
		}
		if(clinicCost+doctorCost+excutorCost > 100) {
			layer.msg("抽成总额超过100%,不合法", {icon : 2,time : 1000,});	
			return false;
		}
		return true;
	}
	
	//如果没有修改直接显示修改成功
	//param:修改后的值；data:原来的值
	//true :未修改；false:修改 需要插入数据库
	function isAlter(param,data) {
		//console.log("isAlter param");
		//console.log(param);
		//console.log("isAlter data");
		//console.log(data);
		var category = localStorage.getItem("projectTable_category");
		//如果是治疗项目
		if(category == 1) {
			var doctorCost = parseFloat(param.doctorCost);
			var clinicCost = parseFloat(param.clinicCost);
			var excutorCost = parseFloat(param.excutorCost); 
			if(param.status == data.statusInt 
					&& param.type == data.typeInt 
					&& param.name == data.projectName
					&& doctorCost == data.doctorCost 
					&& clinicCost == data.clinicCost 
					&& excutorCost == data.excutorCost) {
				layer.msg("修改成功",{icon:1,time:1000,});
				return true;
			}
		}
		//如果说是费用项目
		if(category == 2) {
			if(param.status == data.statusInt 
					&& param.type == data.typeInt 
					&& param.name == data.projectName) {
				layer.msg("修改成功",{icon:1,time:1000,});
				return true;
			}
		}
		return false;
	};
	
</script>
</html>
